﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using smartphone.App_Code;
using System.Data;
using System.Data.Common;

namespace smartphone.UserControls
{
    public partial class NewProduct : System.Web.UI.UserControl
    {
        Product product = new Product();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetNewProducts();
            }
        }

        private void GetNewProducts()
        {
            string page = Request.QueryString["Page"];
            if (page == null)
            {
                page = "1";
            }

            RpProductsList.DataSource = product.GetNewProducts();
            RpProductsList.DataBind();

        }

        protected void RpProductsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
                e.Item.ItemType == ListItemType.AlternatingItem)
            {
                // lay doi tuong Image chua hinh khuyen mai
                Label deal = (Label)e.Item.FindControl("LabelDeal");

                // lay gia tri xem co khuyen mai hay khong
                string hasDeal = ((DataRowView)e.Item.DataItem).Row.ItemArray[5].ToString();

                string strStart = String.Format(((DataRowView)e.Item.DataItem).Row.ItemArray[7].ToString(), "yyyy-MM-dd");
                string strEnd = String.Format(((DataRowView)e.Item.DataItem).Row.ItemArray[8].ToString(), "yyyy-MM-dd");
                DateTime startDate = new DateTime();
                DateTime endDate = new DateTime();

                //lay ngay hien tai so san voi ngay bat dau vao ngay ket thuc xem khuyen mai co con hieu luc hay khong

                DateTime now = DateTime.Now;

                //kiem tra xem co khuyen mai hay khong vao thu thi so sanh
                if (strStart != "" && strEnd != "")
                {
                    //lay ngay bat dau khuyen mai
                    //startDate = DateTime.ParseExact(strStart, "yyyy-MM-dd", null);
                    startDate = DateTime.Parse(strStart);
                    //lay ngay ket thuc khuyen mai
                    //endDate = DateTime.ParseExact(strEnd, "yyyy-MM-dd", null);
                    endDate = DateTime.Parse(strEnd);

                    if (hasDeal != "" && DateTime.Compare(startDate, now) < 0 && DateTime.Compare(now, endDate) < 0)
                    {
                        // Neu co khuyen mai thi hien thi hinh anh.
                        deal.Visible = true;
                    }
                }
            }

            //lay thong tin ve thuoc tinh cua tung san pham bang cach bat su kien databound
            DataRowView dataRow = (DataRowView)e.Item.DataItem;
            string productID = dataRow["maSanPHam"].ToString();
            DataTable attrTable = product.GetProductAttributes(Int32.Parse(productID));

            string prevAttr = "";
            string attrName, attrValue, attrValueId;

            // lay placeholder chua cac thuoc tinh
            PlaceHolder attrPlaceHolder = (PlaceHolder)e.Item.FindControl("attrPlaceHolder");

            Label attrNameLabel;
            DropDownList attrValueDropDownList = new DropDownList();

            foreach (DataRow row in attrTable.Rows)
            {
                attrName = row["tenThuocTinh"].ToString();
                attrValue = row["giaTriThuocTinh"].ToString();
                attrValueId = row["maGiaTriThuocTinh"].ToString();

                if (attrName != prevAttr)
                {
                    prevAttr = attrName;
                    attrNameLabel = new Label();
                    attrNameLabel.Text = attrName + " ";

                    attrValueDropDownList = new DropDownList();
                    attrPlaceHolder.Controls.Add(attrNameLabel);
                    attrPlaceHolder.Controls.Add(attrValueDropDownList);
                }
                attrValueDropDownList.Items.Add(new ListItem(attrValue, attrValueId));
            }
        }

        protected void LinkAddToCart_Click(object sender, EventArgs e)
        {
            //Lay linkbutton duoc click vao 
            LinkButton myButton = sender as LinkButton;
            //Lay commandArgument duoc dua vao trong file aspx
            string id = myButton.CommandArgument.ToString();

            //Lay datalistItem chua linkbutton qua thuoc tinh NamingCOntainer
            RepeaterItem repeatItem = (RepeaterItem)myButton.NamingContainer;
            //Lay placeHolder chua cac control thuoc tinh trong DataListItem
            PlaceHolder attr = (PlaceHolder)RpProductsList.Items[repeatItem.ItemIndex].FindControl("attrPlaceHolder");
            string options = "";

            foreach (Control ctr in attr.Controls)
            {
                //lay ten loai thuoc tinh
                if (ctr is Label)
                {
                    Label lb = (Label)ctr;
                    options += lb.Text;
                }
                //lay gia tri cua thuoc tinh
                if (ctr is DropDownList)
                {
                    DropDownList dr = (DropDownList)ctr;
                    options += dr.Items[dr.SelectedIndex] + "; ";
                }
            }

            ShoppingCart shop = new ShoppingCart();
            shop.AddItem(id, options);
        }

    }
}